package com.qf.j2112.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.j2112.po.SysCart;
import com.qf.j2112.po.SysOrder;
import com.qf.j2112.po.SysOrderitem;
import com.qf.j2112.service.SysCartService;
import com.qf.j2112.service.SysOrderService;
import com.qf.j2112.service.SysOrderitemService;
import com.qf.j2112.vo.Cartvo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qf.j2112.mapper.SysOrderMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 订单Service业务层处理
 * @author j2112
 * @date 2022-04-12
 */

@Service
@Slf4j
//开启事务
@Transactional
public class SysOrderServiceImpl implements SysOrderService {

    @Resource
    private SysOrderMapper sysOrderMapper;

    @Autowired
    private SysOrderitemService sysOrderitemService;

    @Autowired
    private SysCartService sysCartService;


    /**
     * 查询订单
     * @param orderId 订单主键
     * @return 订单实体
     */
    @Override
    public SysOrder selectSysOrderByOrderId(Integer orderId) {
        return sysOrderMapper.selectSysOrderByOrderId(orderId);
    }

    /**
     * 查询订单列表
     * @return 订单
     */
    @Override
    public List<SysOrder> selectSysOrderList() {
        return sysOrderMapper.selectSysOrderList();
    }

    @Override
    public List<SysOrder> selectSysOrderListByUserId(Integer UserId) {
        return sysOrderMapper.selectSysOrderListByUserId(UserId);
    }

    //新增订单
    //主键回填
    @Override
    public SysOrder insertSysOrder(Cartvo cartvo) {
        String name=null;
        List<SysCart> cartList = cartvo.getGoods();
        int size=cartList.size();
        if(size==1){
            name=cartList.get(0).getCartGoodsName();
        }else {
            name=cartList.get(0).getCartGoodsName()+"等"+size+"项物品";
        }
        SysOrder sysOrder=new SysOrder();
        sysOrder.setOrderUserId(cartvo.getUserId());
        sysOrder.setOrderName(name);
        sysOrder.setOrderPaidmoney(cartvo.getTotalMoney());
        sysOrder.setOrderContactId(cartvo.getChosenContactId());
        sysOrder.setOrderYiZhanId(cartvo.getChosenYiZhanId());
        sysOrder.setOrderMessage(cartvo.getMessage());
        sysOrder.setOrderCreatetime(new Date());
        sysOrder.setOrderJifen((int) (cartvo.getTotalMoney()*0.1));
        //待支付
        sysOrder.setOrderOrderstatusId(0);
        int i= sysOrderMapper.insertSysOrder(sysOrder);
        if(i>-1) {
            for (SysCart cart : cartList) {
                SysOrderitem sysOrderitem = new SysOrderitem();
                sysOrderitem.setOrderitemOrderId(sysOrder.getOrderId());
                sysOrderitem.setOrderitemGoodsId(cart.getCartGoodsId());
                sysOrderitem.setOrderGoodsName(cart.getCartGoodsName());
                sysOrderitem.setOrderitemPrice(cart.getCartGoodsPrice());
                sysOrderitem.setOrderitemNum(cart.getCartGoodsNum());
                sysOrderitem.setOrderitemPic(cart.getCartGoodsPic());
                sysOrderitem.setOrderitemTotal(cart.getCartGoodsPrice() * cart.getCartGoodsNum());
                //插入订单详情
                sysOrderitemService.insertSysOrderitem(sysOrderitem);
            }
            //清掉购物车
            int m = sysCartService.deleteSysCartByUserId(cartvo.getUserId());

        }
        return sysOrder;
    }

    /**
     * 修改订单
     * @param sysOrder 订单
     * @return boolean 结果
     */
    @Override
    public Boolean updateSysOrder(SysOrder sysOrder) {
        int i=sysOrderMapper.updateSysOrder(sysOrder);
        if(i>0){
            return true;
        }
        return false;
    }

    /**
     * 批量删除订单
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteSysOrderByOrderIds(Integer[] orderIds) {
        return sysOrderMapper.deleteSysOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteSysOrderByOrderId(Integer orderId)
    {
        return sysOrderMapper.deleteSysOrderByOrderId(orderId);
    }

    @Override
    public List<SysOrder> selectOrder(Integer size, Integer page, Integer status1, Integer status2) {
        PageHelper.startPage(page,size);
        log.info("参数为"+page+size+status1+status2);
        List<SysOrder> list=sysOrderMapper.selectOrderByStatus(status1,status2);
        for(SysOrder order:list){
            SysOrderitem sysOrderitem=new SysOrderitem();
            sysOrderitem.setOrderitemOrderId(order.getOrderId());
            List<SysOrderitem> sysOrderitems=sysOrderitemService.selectSysOrderitemList(sysOrderitem);
            order.setOrderitems(sysOrderitems);
        }
        return list;
    }

    @Override
    public PageInfo<SysOrder> selectOrderNum2(Integer size, Integer page, Integer status1, Integer status2) {
        PageHelper.startPage(page,size);
        List<SysOrder> list=sysOrderMapper.selectOrderByStatus(status1,status2);
        PageInfo<SysOrder> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
}
